using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

using GonzalesCore.CoreElements;
using GonzalesCore.CoreManagers;
using Kypris2.CoreElements;
using Kypris2.Utils;
using Jamila2.Tools;

public partial class GonzalesViewClaimNameList : System.Web.UI.Page
{
    #region Variables

    protected PrivateLoginSession CurrentPrivateSession = null;
    protected LogManager CurLogManager = null;
    protected GonzalesClaimHeader ResultObj = null;
    protected EntertainmentHeader EntObj = null;
    protected readonly int TotalColumns = 5;

    #region ClaimHavingCreditCard
    protected bool ClaimHavingCreditCard
    {
        get
        {
            return (bool)ViewState["ClaimHavingCreditCard"];
        }

        set
        {
            ViewState["ClaimHavingCreditCard"] = value;
        }
    }
    #endregion

    #region CurrentClaimStatus

    protected GonzalesClaimHeader.TypeOfClaimStatus CurrentClaimStatus
    {
        get
        {
            return (GonzalesClaimHeader.TypeOfClaimStatus)ViewState["CurrentClaimStatus"];
        }

        set
        {
            ViewState["CurrentClaimStatus"] = value;
        }
    }

    #endregion

    #region ClaimInternalID
    protected string ClaimInternalID
    {
        get
        {
            return ViewState["ClaimInternalID"].ToString();
        }

        set
        {
            ViewState["ClaimInternalID"] = value;
        }
    }
    #endregion

    #region EntertainmentID

    protected string EntertainmentID
    {
        get
        {
            return ViewState["EntertainmentID"].ToString();
        }

        set
        {
            ViewState["EntertainmentID"] = value;
        }
    }

    #endregion

    #region ResultTable
    protected DataTable ResultTable
    {
        get
        {
            return (DataTable)ViewState[GonzalesCommonNamingConvention.CurrentPrivateSessionViewState];
        }

        set
        {
            ViewState[GonzalesCommonNamingConvention.CurrentPrivateSessionViewState] = value;
        }
    }
    #endregion

    #region Common Variables for Language Translation

    #region Item_Approvedby
    public string Item_Approvedby
    {
        get
        {
            return ViewState["Item_Approvedby"].ToString();
        }

        set
        {
            ViewState["Item_Approvedby"] = value;
        }
    }
    #endregion

    #region Item_RejectedByApprover
    public string Item_RejectedByApprover
    {
        get
        {
            return ViewState["Item_RejectedByApprover"].ToString();
        }

        set
        {
            ViewState["Item_RejectedByApprover"] = value;
        }
    }
    #endregion

    #region Item_RejectedByFinance
    public string Item_RejectedByFinance
    {
        get
        {
            return ViewState["Item_RejectedByFinance"].ToString();
        }

        set
        {
            ViewState["Item_RejectedByFinance"] = value;
        }
    }
    #endregion

    #region TotalCost
    public string TotalCost
    {
        get
        {
            return ViewState["TotalCost"].ToString();
        }

        set
        {
            ViewState["TotalCost"] = value;
        }
    }
    #endregion

    #region No
    public string No
    {
        get
        {
            return ViewState["No"].ToString();
        }

        set
        {
            ViewState["No"] = value;
        }
    }
    #endregion

    #region Attendees
    public string Attendees
    {
        get
        {
            return ViewState["Attendees"].ToString();
        }

        set
        {
            ViewState["Attendees"] = value;
        }
    }
    #endregion

    #region Titles
    public string Titles
    {
        get
        {
            return ViewState["Titles"].ToString();
        }

        set
        {
            ViewState["Titles"] = value;
        }
    }
    #endregion

    #region Affiliations
    public string Affiliations
    {
        get
        {
            return ViewState["Affiliations"].ToString();
        }

        set
        {
            ViewState["Affiliations"] = value;
        }
    }
    #endregion

    #region CostPerHead
    public string CostPerHead
    {
        get
        {
            return ViewState["CostPerHead"].ToString();
        }

        set
        {
            ViewState["CostPerHead"] = value;
        }
    }
    #endregion

    #endregion

    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
        this.SessionCheck();

        if (!IsPostBack)
        {
            #region Request.QueryString["id"]
            if (Request.QueryString["id"] != null && Request.QueryString["id"].Length == 36)
            {
                this.ClaimInternalID = Request.QueryString["id"];
            }
            else
            {
                Response.Redirect(GonzalesErrorPageRedirect.PageFlowError);
            }
            #endregion

            this.SwitchLanguage();
            this.PageComponentSetup();

            this.LoadData();

        }
    }
    #endregion

    #region Customized Methods

    #region SessionCheck
    public void SessionCheck()
    {

        if (Session[GonzalesCommonNamingConvention.CurrentPrivateSession] != null)
        {
            this.CurrentPrivateSession = (PrivateLoginSession)Session[GonzalesCommonNamingConvention.CurrentPrivateSession];
            this.CurLogManager = new LogManager(this.CurrentPrivateSession);    // activate Log Manager
        }
        else
        {
            Response.Redirect(GonzalesErrorPageRedirect.SessionError);
        }

    }
    #endregion

    #region PageComponentSetup
    protected void PageComponentSetup()
    {
        this.Button_Print.Attributes["onclick"] = "print();return false;";
        this.Button1.Attributes["onclick"] = "print();return false;";
    }
    #endregion

    #region SwitchLanguage
    protected void SwitchLanguage()
    {
        try
        {
            using (MultiLingualFactory CurLanguageFactory = MultiLingualEngine.GetMultiLingualFactory(this, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage.InternalID))
            {


                this.Label_PageTitle.Text = CurLanguageFactory.DisplayValue("PageTitle");


                this.Label_EmployeeName.Text = CurLanguageFactory.DisplayValue("EmployeeName") + ":";
                this.Label_EmployeeNo.Text = CurLanguageFactory.DisplayValue("EmployeeNo") + ":";
                this.Label_ClaimNum.Text = CurLanguageFactory.DisplayValue("ClaimID") + ":";
                this.Label_Purpose.Text = CurLanguageFactory.DisplayValue("Purpose") + ":";
                this.Label_Status.Text = CurLanguageFactory.DisplayValue("Status") + ":";
                this.Label_Approver.Text = CurLanguageFactory.DisplayValue("Approver") + ":";
                this.Label_ProfitCenter.Text = CurLanguageFactory.DisplayValue("ProfitCenter") + ":";

                this.Item_Approvedby = CurLanguageFactory.DisplayValue("ApprovedBy") + ":";
                this.Item_RejectedByApprover = CurLanguageFactory.DisplayValue("RejectedByApprover") + ":";
                this.Item_RejectedByFinance = CurLanguageFactory.DisplayValue("RejectedByFinance") + ":";

                this.Button1.Text = CurLanguageFactory.DisplayValue("Button1");
                this.Button_Print.Text = CurLanguageFactory.DisplayValue("Button1");
                this.Label_NameList.Text = CurLanguageFactory.DisplayValue("NameList");

                this.TotalCost = CurLanguageFactory.DisplayValue("TotalCost");
                this.No = CurLanguageFactory.DisplayValue("No");
                this.Titles = CurLanguageFactory.DisplayValue("Titles");
                this.Attendees = CurLanguageFactory.DisplayValue("Attendees");
                this.Affiliations = CurLanguageFactory.DisplayValue("Affiliations");
                this.CostPerHead = CurLanguageFactory.DisplayValue("CostPerHead");
            }
        }
        catch (Exception ex)
        {
            this.CurLogManager.LogError("[GonzalesViewClaim.SwitchLanguage.EX] : " + ex.Message, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.User);
        }

    }
    #endregion

    #endregion

    #region Data Operation

    #region LoadData
    protected void LoadData()
    {
        using (ClaimManager CurClaimManager = new ClaimManager(this.CurrentPrivateSession))
        {
            ResultObj = CurClaimManager.GetClaimByInternalID(this.ClaimInternalID);
            ResultObj.ClaimDetails = CurClaimManager.GetClaimItemsByClaimHeaderInLang(this.ClaimInternalID, this.CurrentPrivateSession.CurrentLoggedOnUser.PreferedLanguage);
            ResultObj.ClaimDetails.SortByOrder();

            this.CurrentClaimStatus = ResultObj.Status;

            using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
            {
                this.ResultObj.ClaimedBy = CurUserManager.GetUserByInternalID(this.ResultObj.ClaimedBy.InternalID);
            }

            // fill in the blank
            this.LabelValue_EmployeeName.Text = ResultObj.ClaimedBy.DisplayName + " (" + ResultObj.ClaimedBy.EmailID + ")";
            this.LabelValue_EmployeeNo.Text = ResultObj.ClaimedBy.EmployeeID;
            this.LabelValue_ClaimNo.Text = ResultObj.ClaimID;
            this.LabelValue_Purpose.Text = ResultObj.Reasons;
            this.LabelValue_Status.Text = ClaimManager.GetClaimStatusInString(ResultObj.Status);
            this.LabelValue_ProfitCenter.Text = ResultObj.ClaimedBy.CostCenter;

            #region Status & Related Personnel

            using (UserManager CurUserManager = new UserManager(this.CurrentPrivateSession))
            {
                SystemUser CurUserObj = null;

                if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByApprover)
                {
                    CurUserObj = CurUserManager.GetUserByInternalID(ResultObj.LastUpdatedBy.InternalID);

                    this.Label_Approver.Text = this.Item_RejectedByApprover;
                    this.LabelValue_Approver.Text = CurUserObj.DisplayName;
                }
                else if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByAuthorizer)
                {
                    CurUserObj = CurUserManager.GetUserByInternalID(ResultObj.LastUpdatedBy.InternalID);

                    this.Label_Approver.Text = this.Item_RejectedByFinance;
                    this.LabelValue_Approver.Text = CurUserObj.DisplayName;
                }
                else if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinance)
                {
                    CurUserObj = CurUserManager.GetUserByInternalID(ResultObj.LastUpdatedBy.InternalID);

                    this.Label_Approver.Text = this.Item_RejectedByFinance;
                    this.LabelValue_Approver.Text = CurUserObj.DisplayName;
                }
                else if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.RejectedByFinanceApprover)
                {
                    CurUserObj = CurUserManager.GetUserByInternalID(ResultObj.LastUpdatedBy.InternalID);

                    this.Label_Approver.Text = this.Item_RejectedByFinance;
                    this.LabelValue_Approver.Text = CurUserObj.DisplayName;
                }
                else if (ResultObj.Status == GonzalesClaimHeader.TypeOfClaimStatus.Submitted)
                {
                    CurUserObj = CurUserManager.GetUserByInternalID(ResultObj.NextApprover.InternalID);

                    this.LabelValue_Approver.Text = CurUserObj.DisplayName;
                }
                else
                {
                    if (ResultObj.NextApprover != null)
                    {
                        CurUserObj = CurUserManager.GetUserByInternalID(ResultObj.NextApprover.InternalID);
                        this.LabelValue_Approver.Text = CurUserObj.DisplayName;

                    }
                }

            }

            #endregion

            if (ResultObj.ClaimDetails != null)
            {
                DataRow RowObj = null;
                this.ClaimHavingCreditCard = false;

                for (int x = 0; x < ResultObj.ClaimDetails.Count; x++)
                {
                    
                    using (ClaimManager CurClaimEntertainmentManager = new ClaimManager(this.CurrentPrivateSession))
                    {
                        EntObj = CurClaimEntertainmentManager.GetEntertainmentHeader(this.ClaimInternalID, ResultObj.ClaimDetails[x].InternalID);

                        if (EntObj != null)
                        {
                            this.EntertainmentID = EntObj.InternalID;

                            TableRow NewTableHeaderRow = new TableRow();

                            NewTableHeaderRow.Cells.Add(new TableCell());
                            NewTableHeaderRow.Cells[0].CssClass = "GridViewItem";
                            NewTableHeaderRow.Cells[0].ColumnSpan = 5;
                            NewTableHeaderRow.Cells[0].Font.Bold = true;
                            NewTableHeaderRow.Cells[0].HorizontalAlign = HorizontalAlign.Left;
                            NewTableHeaderRow.Cells[0].Text = this.Label_Purpose.Text + ":" + this.EntObj.Purpose.ToString();

                            this.Table_NameList.Rows.Add(NewTableHeaderRow);

                            TableRow NewTableHeaderRow2 = new TableRow();

                            NewTableHeaderRow2.Cells.Add(new TableCell());
                            NewTableHeaderRow2.Cells[0].CssClass = "GridViewItem";
                            NewTableHeaderRow2.Cells[0].ColumnSpan = 5;
                            NewTableHeaderRow2.Cells[0].Font.Bold = true;
                            NewTableHeaderRow2.Cells[0].HorizontalAlign = HorizontalAlign.Left;
                            NewTableHeaderRow2.Cells[0].Text = this.TotalCost + this.EntObj.TotalCost.ToString();

                            this.Table_NameList.Rows.Add(NewTableHeaderRow2);

                            TableRow NewTableDetailRow = new TableRow();

                            #region Display the Name List Header

                            for (int p = 0; p < this.TotalColumns; p++)
                            {
                                NewTableDetailRow.Cells.Add(new TableCell());
                                NewTableDetailRow.Cells[p].CssClass = "GridViewItem";
                                NewTableDetailRow.Cells[p].HorizontalAlign = HorizontalAlign.Center;
                            }

                            NewTableDetailRow.Cells[0].Text = this.No;
                            NewTableDetailRow.Cells[1].Text = this.Attendees;
                            NewTableDetailRow.Cells[2].Text = this.Titles;
                            NewTableDetailRow.Cells[3].Text = this.Affiliations;
                            NewTableDetailRow.Cells[4].Text = this.CostPerHead;

                            this.Table_NameList.Rows.Add(NewTableDetailRow);

                            #endregion

                            // load the items
                            this.EntObj.Details = CurClaimEntertainmentManager.GetEntertainmentDetails(EntObj.InternalID);

                            if (this.EntObj.Details == null && CurClaimManager.ErrorMessage.Length > 0)
                            {
                                string LogID = SwissArmy.UniqueID();
                                this.CurLogManager.LogError(LogID, "[GonzalesToolsEntertainment : LoadData] : CurClaimManager.GetEntertainmentDetails('" + ResultObj.InternalID + "') : " + CurClaimManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                            }

                            int curRow = 1;

                            for (int y = 0; y < EntObj.Details.Count; y++)
                            {
                                int cellCounter = 0;

                                TableRow NewTableRow = new TableRow();

                                for (int z = 0; z < this.TotalColumns; z++)
                                {
                                    // set default setting to all the row
                                    NewTableRow.Cells.Add(new TableCell());
                                    NewTableRow.Cells[z].CssClass = "GridViewItem";
                                    NewTableRow.Cells[z].HorizontalAlign = HorizontalAlign.Center;
                                }

                                NewTableRow.Cells[cellCounter].Text =curRow.ToString();
                                curRow++;
                                cellCounter++;
                                NewTableRow.Cells[cellCounter].Text = EntObj.Details[y].Attendess.ToString();
                                cellCounter++;
                                NewTableRow.Cells[cellCounter].Text = EntObj.Details[y].Title.ToString();
                                cellCounter++;
                                NewTableRow.Cells[cellCounter].Text = EntObj.Details[y].Company.ToString();
                                cellCounter++;
                                NewTableRow.Cells[cellCounter].Text = EntObj.Details[y].CostPerHead.ToString();
                                cellCounter++;

                                this.Table_NameList.Rows.Add(NewTableRow);
                                
                            }

                        }
                        else if (CurClaimManager.ErrorMessage.Length > 0)
                        {
                            // log reasons
                            string LogID = SwissArmy.UniqueID();
                            this.CurLogManager.LogError(LogID, "[GonzalesToolsEntertainment : LoadData] : CurClaimManager.GetEntertainmentHeader('" + this.ClaimInternalID + "', '" + EntObj.InternalID + "') : " + CurClaimManager.ErrorMessage, this.CurrentPrivateSession.CurrentLoggedOnUser, LogInterface.System);
                        }
                        else
                        {
                            // this is a new entertainment list
                        }

                    }
                }
            }

        }
    }

    #endregion

    #endregion

}
